//
//  MyOrderViewController.m
//  jingdongfang
//
//  Created by 郝志宇 on 16/6/24.
//  Copyright © 2016年 XuDong Jin. All rights reserved.
//

#import "MyOrderViewController.h"
#import "ListTableViewCell.h"
#import "OrderFooter.h"
#import "OrderHeader.h"
#import "OrderXQViewController.h"
#import "IGMyorderListApi.h"
#import "OrderDetailModel.h"

@interface MyOrderViewController () <UITableViewDelegate, UITableViewDataSource, DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
{
    NSString *pageNumber;
}

@property (weak, nonatomic) IBOutlet UITableView *table;
@property(nonatomic,strong)IGMyorderListApi *mySellApi;
@property (nonatomic, strong) NSMutableArray *dataArray;

@end

@implementation MyOrderViewController
#pragma mark -懒加载
-(NSMutableArray *)dataArray
{
    if (!_dataArray)
    {
        _dataArray=[NSMutableArray array];
    }
    return _dataArray;
}
#pragma mark -生命周期
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self firstPage];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.table.backgroundColor = [UIColor c2Color];
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.table.width, 10)];
    view.backgroundColor = self.table.backgroundColor;
    self.table.tableHeaderView = view;
    [self.table registerClass:[ListTableViewCell class] forCellReuseIdentifier:@"oredrCell"];
    [self.table registerClass:[OrderHeader class] forHeaderFooterViewReuseIdentifier:@"header"];
    [self.table registerClass:[OrderFooter class] forHeaderFooterViewReuseIdentifier:@"footer"];
    [self.table ig_setHeaderWithRefreshingBlock:^{
        [self firstPage];
    }];
    self.table.emptyDataSetSource = self;
    self.table.emptyDataSetDelegate = self;
    self.mySellApi = [IGMyorderListApi new];
    [self firstPage];
}

- (void)firstPage
{
    pageNumber = @"1";
    [self requestData];
}

-(void)requestData
{
    _mySellApi.type = @"1";
    _mySellApi.pageNumber = pageNumber;
    _mySellApi.pageSize = @"10";
    @KWeakObj(self)
    [self.mySellApi startWithDecodeClass:[IGMyorderListResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        [self.table ig_endHeaderRefreshing];
        [self.table ig_endFooterRefreshing];
        if (responseObject) {
            IGMyorderListResp *resp=responseObject;
            IGMyorderListModel *model= resp.data;
            if (model.orders && model.orders.count > 0)
            {
                [selfWeak setupFooter];
            }
            else
            {
                [selfWeak.table ig_endFooterRefreshingWithNoMoreData];
            }
            if ([pageNumber isEqualToString:@"1"])
            {
                _dataArray = [NSMutableArray arrayWithArray:model.orders];
            }
            else
            {
                [_dataArray addObjectsFromArray:model.orders];
            }
            [self.table reloadData];
        }
    }];
}

- (void)setupFooter
{
    @KWeakObj(self)
    [self.table ig_setFooterWithRefreshingBlock:^{
        pageNumber = [NSString stringWithFormat:@"%ld", [pageNumber integerValue] + 1];
        [selfWeak requestData];
    }];
}

- (void)scrollToTOP
{
    [self.table setContentOffset:CGPointMake(0, 0) animated:YES];
}

#pragma mark - UITableViewDelegate, UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArray.count?:0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"oredrCell"];
    if (cell==nil) {
        cell = [[ListTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"oredrCell"];
    }
    OrderDetailModel *info = _dataArray[indexPath.section];
    if ([info.status integerValue]==0||[info.status integerValue]==2||[info.status integerValue]==3||[info.status integerValue]==4||[info.status integerValue]==6) {
        cell.selectionStyle=UITableViewCellSelectionStyleGray;
    }else{
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
    }
    cell.backColor=@"1";
    cell.data = info;
    [cell setNeedsLayout];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    OrderDetailModel *info = _dataArray[indexPath.section];
        OrderXQViewController *vc = [[OrderXQViewController alloc] init];
        vc.oid=info.oId;
        vc.isMyBuy=NO;
        [UIViewController ig_pushViewController:vc animated:YES];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    OrderDetailModel *info = _dataArray[section];
    OrderHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    CGFloat h = [self tableView:tableView heightForHeaderInSection:section];
    if (header==nil)
    {
        header = [[OrderHeader alloc] initWithReuseIdentifier:@"header"];
    }
    header.frame = CGRectMake(0, 0, tableView.width, h);
    header.info = info;
    header.isMyBuy=NO;
    [header setNeedsLayout];
    return header;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    OrderDetailModel *info = _dataArray[section];
    OrderFooter *footer = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"footer"];
    CGFloat h = [self tableView:tableView heightForFooterInSection:section];
    if (footer==nil)
    {
        footer = [[OrderFooter alloc] initWithReuseIdentifier:@"footer"];
    }
    footer.frame = CGRectMake(0, 0, tableView.width, h);
    footer.info =info;
    footer.isMyBuy=NO;
    [footer setNeedsLayout];
    return footer;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

- (NSAttributedString *)descriptionForEmptyDataSet:(UIScrollView *)scrollView {
//    return [[NSAttributedString alloc] initWithString:@"您还没有出售任何物品~" attributes:nil];
    return [[NSAttributedString alloc] initWithString:NSLocalizedString(@"No Works Sold", nil) attributes:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
